﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service;
using Model;
namespace 企业门户.Controllers
{
    public class UserController : Controller
    {
        Model1 db = new Model1();
        UserService userService = new UserService();
        // GET: User

        //登录
        [HttpGet, Route("login")]
        public ActionResult login()
        {
            return View();
        }

        [HttpPost, Route("login")]
        public ActionResult login(string uname, string upwd, string ReturnUrl)
        {
            var a = db.CMS_User.Where(x => x.uname == uname && x.upwd == upwd).FirstOrDefault();
            if (a != null)
            {
                //登录成功
                CMS_User stu = new CMS_User();
                stu.uname = uname;
                stu.upwd = upwd;
                //记录登录身份信息
                Session["user"] = stu;
                //跳转到来访页面
                if (string.IsNullOrEmpty(ReturnUrl))
                    ReturnUrl = "/Home/Index";
                return Redirect(ReturnUrl);
            }
            else
            {
                //登录失败
                ViewBag.ErrorMessage = "用户名或者密码错误！";
                return View();

            }
        }

        //登出
        [Route("logout")]
        public ActionResult Logout()
        {
            //清除身份信息
            Session["user"] = null;
            //跳转到登录页面
            return Redirect("/User/login");
        }


        //注册
        [HttpGet]
        public ActionResult register()
        {
            return View();
        }

        [HttpPost]
        public ActionResult register(string pwd,CMS_User u)
        {
            if (u.uname!=null && u.upwd!=null && u.mobile!=null&&pwd==u.upwd)
            {
                userService.register(u);
                return View();
            }
            else
            {
                
                ViewBag.ErrorMessage = "请输入正确格式！";
                return View();
            }
           
        }

        //销毁
        protected override void Dispose(bool disposing)
        {
            userService.Dispose();
            db.Dispose();
        }
    }
}